package com.cc.wechat.controller;

import com.cc.wechat.common.ApiResponse;
import com.cc.wechat.common.constants.ContactConstants;
import com.cc.wechat.dto.request.AddFriendRequest;
import com.cc.wechat.dto.request.HandleFriendRequest;
import com.cc.wechat.dto.response.FriendResponse;
import com.cc.wechat.service.ContactService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 好友关系控制器
 */
@RestController
@RequestMapping("/api/contacts")
@RequiredArgsConstructor
public class ContactController {

    private final ContactService contactService;

    /**
     * 发送好友请求
     */
    @PostMapping("/requests")
    public ApiResponse<Void> sendFriendRequest(@RequestBody @Validated AddFriendRequest request) {
        contactService.sendFriendRequest(request);
        return ApiResponse.success(ContactConstants.Message.ADD_FRIEND_SUCCESS, null);
    }

    /**
     * 获取收到的好友请求列表
     */
    @GetMapping("/requests/received")
    public ApiResponse<List<FriendResponse>> getReceivedFriendRequests() {
        return ApiResponse.success(contactService.getReceivedFriendRequests());
    }

    /**
     * 获取发送的好友请求列表
     */
    @GetMapping("/requests/sent")
    public ApiResponse<List<FriendResponse>> getSentFriendRequests() {
        return ApiResponse.success(contactService.getSentFriendRequests());
    }

    /**
     * 处理好友请求
     */
    @PutMapping("/requests/{requestId}")
    public ApiResponse<Void> handleFriendRequest(
            @PathVariable Long requestId,
            @RequestBody @Validated HandleFriendRequest request) {
        contactService.handleFriendRequest(requestId, request);
        String message = request.getStatus() == ContactConstants.Status.ACCEPTED ? 
            ContactConstants.Message.ACCEPT_FRIEND_SUCCESS : 
            ContactConstants.Message.REJECT_FRIEND_SUCCESS;
        return ApiResponse.success(message, null);
    }

    /**
     * 获取好友列表
     */
    @GetMapping("/friends")
    public ApiResponse<List<FriendResponse>> getFriendList() {
        return ApiResponse.success(contactService.getFriendList());
    }

    /**
     * 删除好友
     */
    @DeleteMapping("/friends/{friendId}")
    public ApiResponse<Void> deleteFriend(@PathVariable Long friendId) {
        contactService.deleteFriend(friendId);
        return ApiResponse.success(ContactConstants.Message.DELETE_FRIEND_SUCCESS, null);
    }

    /**
     * 更新好友备注
     * @param friendId 好友ID
     * @param remark 新的备注
     * @return ApiResponse<Void> 返回处理结果
     */
    @PutMapping("/friends/{friendId}/remark")
    public ApiResponse<Void> updateRemark(
            @PathVariable Long friendId,
            @RequestParam String remark) {
        contactService.updateRemark(friendId, remark);
        return ApiResponse.success("备注更新成功", null);
    }
} 